import re
import json

#参数化基类
class Paramete:
    pass


#框架中的代码，在平台退休，无法适应多并发
def replace_data(data):
    """
    替换变量
    :param data:
    :return:
    """
    ru = r'\${(.*?)}'
    while re.search(ru, data):
        res = re.search(ru, data)
        item = res.group()
        keys = res.group(1)

        # 先找系统环境变量，如果有则替换；如果没有则找临时变量
        try:
            value = getattr(Paramete, keys).decode('unicode_escape')
        except Exception as e:
            try:
                value = getattr(Paramete, keys)
            except Exception as e:
                return data
        finally:
            try:
                if keys == 'response':
                    d_header = dict(value.headers)
                    d_resbody = json.loads(value.text)
                    d_response = dict(d_header)
                    d_response.update(d_resbody)
                    value_1 = json.dumps(d_response).encode('utf-8').decode('unicode_escape')
                    data = re.sub(ru, value_1, data, 1)
                else:
                    data = re.sub(ru, value, data, 1)
            except Exception as e:
                return data
    return data

def analyzing_param(param):
    """
     ${abc}取出abc
    :param param:
    :return:
    """
    ru = r'\${(.*?)}'
    if re.search(ru, param):
        return re.findall(ru, param)[0]
    return param


#平台多并发，参数化管理器
class Manageattr(object):
    def __init__(self):
        self.keys = []    #记录变量
        self.params = Paramete()    #线程变量管理


    def setattr(self, key, value):
        """
        添加临时变量
        """
        try:
            setattr(self.params, key, value)
            self.keys.append(key)
            return True, "成功"
        except Exception as e:
            return False, str(e)

    def getattr(self, key):
        """
        添加临时变量
        """
        try:
            value = getattr(self.params, key)
            return True, value
        except Exception as e:
            return False, str(e)

    def delattrs(self):
        """
        清空临时变量，用了多线程后，可要可不要了
        """
        for key in self.keys:
            try:
                delattr(self.params, key)
            except Exception as e:
                continue
        self.keys = []
        return True, "成功"


    def replace_data(self, data):
        """
        替换变量
        :param data:
        :return:
        """
        judge = data    #防代码死循环
        ru = r'\${(.*?)}'
        while re.search(ru, judge):
            res = re.search(ru, judge)
            #------------防代码死循环
            data_lst = judge.split(res.group())
            judge = ''
            count = len(data_lst)
            for i in range(1, count):
                if judge == '':
                    judge = judge + data_lst[i]
                else:
                    judge = judge + res.group() + data_lst[i]
            #-------------
            keys = res.group(1)
            # 先找系统环境变量，如果有则替换；如果没有则找临时变量
            try:
                value = getattr(self.params, keys).decode('unicode_escape')
            except Exception:
                try:
                    value = getattr(self.params, keys)
                except Exception:
                    continue
            finally:
                try:
                    if keys == 'response':
                        d_header = dict(value.headers)
                        d_resbody = json.loads(value.text)
                        d_response = dict(d_header)
                        d_response.update(d_resbody)
                        value_1 = json.dumps(d_response).encode('utf-8').decode('unicode_escape')
                        data = re.sub(ru, value_1, data, 1)
                    else:
                        data = re.sub(ru, value, data, 1)
                except Exception:
                    pass
        return data




# print(analyzing_param('phone, pwd'))


# par = Paramete


# setattr(par, 'key', 'value')

# par01 = Paramete
# print(par.key)
# print(par01.key)

# def a():
#     par = Paramete
#     setattr(par, 'key', 'value')
#     print(par.key)
#     delattr(par, 'key')
#
#
# def b():
#     par01 = Paramete
#     print(par01.key)
#
# a()
# b()

# for i in range(1,10):
#     print(i)